package com.rabbit.mq.comtroller;


import com.rabbit.mq.consumer.RabbitMqConsumer;
import com.rabbit.mq.provider.RabbitMqProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;

@RestController
public class RabbitMqController {

    @Autowired
    private RabbitMqProvider rabbitMqProvider;

    @Autowired
    private RabbitMqConsumer rabbitMqConsumer;



    @GetMapping("/sendMessage")
    public void sendMessage(@RequestParam(value = "message") String message, @RequestParam(value = "routKey") String routKey) {
        for (int i = 0; i < 100; i++) {
            rabbitMqProvider.sendMessage("0", message);
            rabbitMqProvider.sendMessage("1", message);
        }
    }

    @GetMapping("/pollMessage")
    public void pollMessage(@RequestParam(value = "routKey") String routKey) throws Exception {
        String key = "ChatMessage" + Math.abs(routKey.hashCode()) % 2;
        HashSet<String> set = rabbitMqConsumer.consumerConfig(key, routKey);
    }


    @GetMapping("/sendTestCru")
    public void send() {
        try {
            long start = System.currentTimeMillis();
            System.out.println("开始时间：      ");
            for (int i = 0; i < 1000000000; i++) {
                TestSend();
            }

            long end = System.currentTimeMillis();
            System.out.println("结束时间：      "+(end - start) / 1000);
        } catch (Exception e) {
            System.out.println("报错；  "+e.getMessage());
        }

    }

    public void TestSend() {
//        ChatMessage chatMsg = ChatMessage.builder()
//                .receiverId("2203156D8G4XFZDG2W0")
//                .senderId("2203156D8G4XFZDG2W1")
//                .msg("")
//                .msgType("image").build();
//
//        NettyData nettyData = NettyData.builder()
//                .action("SingleChatSendImage")
//                .chatMessage(chatMsg)
//                .build();
//        userService.saveMsg(chatMsg, MsgSignFlagEnum.unsign.type);
//        rabbitMqProvider.sendMessage(chatMsg.getReceiverId(), chatMsg.getReceiverId() + ",0");
    }
}
